package org.sample.algorithm.sort;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SleepSort {

    public static void sort(int[] array) {
        ExecutorService pool = Executors.newFixedThreadPool(array.length);

        for (int i = 0; i < array.length; i++)
            pool.execute(new SortUnit(array[i]));

        pool.shutdown();
    }

    private static class SortUnit implements Runnable {
        private int ms;

        public SortUnit(int ms) {
            this.ms = ms;
        }

        public void run() {
            try {
                Thread.sleep(ms * 10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print(ms + ", ");
        }
    }

}